package 排序.utils;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;

public  class Example {
    public  void sort(Comparable[] arr){

    }

    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    public static void exchange(Comparable[] arr, int i, int j) {
        Comparable t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void show(Comparable[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print((arr[i] + " "));

        }
        System.out.println();
    }
    /**
     * 获取待排序数组中的最大值
     * @param arr
     * @return
     */
    public static Comparable getMax(Comparable[] arr){

        Comparable max = arr[0];
        for(int i = 1;i<arr.length;i++) {
            if (less(max, arr[i])) {
                max = arr[i];
            }
        }
        return max;
    }
    /**
     * 获取待排序数组的最小值
     * @param arr
     * @return
     */
    public  static Comparable getMin(Comparable[] arr){

        Comparable min = arr[0];
        for(int i = 1;i<arr.length;i++) {
            if (less(arr[i],i) ) {
                min = arr[i];
            }
        }
        return min;
    }
    public static Comparable getMax(Collection<? extends Comparable> arr){
        return arr.stream().max((o1, o2) -> {
            return o1.compareTo(o2);
        }).get();
    }
    public  static Comparable getMin(Collection<? extends Comparable> arr){
        return arr.stream().min((o1, o2) -> {
            return o1.compareTo(o2);
        }).get();
    }


    public static boolean isSorted(Comparable[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (less(arr[i], arr[i - 1]))
                return false;

        }
        return true;
    }
}
